import { loginAPI, getUserInfoAPI, getUserDetailByIdAPI } from '@/api/user.js'
import * as auth from '@/utils/auth.js'
export default {
  namespaced: true,
  state: {
    // 存储token的变量
    token: auth.getToken() || '',
    // 存储登陆人信息的空对象
    user: {}
  },
  mutations: {
    // 获取token
    setToken (state, token) {
      state.token = token
      auth.setToken(token)
    },
    // 删除token
    delToken (state) {
      state.token = ''
      auth.removeToken()
    },
    // 获取登陆人信息
    setUser (state, user) {
      state.user = user
    },
    // 删除登陆人
    delUser (state) {
      state.user = {}
    }
  },
  actions: {
    // 调接口获取token
    async getToken (ctx, loginForm) {
      const res = await loginAPI(loginForm)
      ctx.commit('setToken', res)
    },
    // 调接口或去登陆人的信息
    async getUser (ctx) {
      const res = await getUserInfoAPI()
      const photo = await getUserDetailByIdAPI(res.userId)
      // console.log(photo)
      ctx.commit('setUser', { ...res, ...photo })
    },
    // 退出登录
    logOut (ctx) {
      ctx.commit('delToken')
      ctx.commit('delUser')
    }
  }
}

